package Product;

import java.io.*;
import java.util.Scanner;

/**need BufferedReader, hasNextProduct(boolean), getNextProduct(return Product)
 *
 * read the product file, check if there is next product and get the
 * next product from the product file.
 * @author Ken Nguyen
 */

public class ProductReader implements Serializable{
    //initialize variables
    Scanner input;

    //read in from products.txt
    public ProductReader(String file) throws FileNotFoundException, IOException{
        input =  new Scanner(new FileReader(file));
    }
    //looping until there is no more product
    public boolean hasNextProduct() {
        return input.hasNextLine();
    }
    //return the product if it is available
   public Product getNextProduct(){
        String line = input.nextLine();
        int upc = Integer.parseInt(line.substring(0, 9).trim());
        String description = line.substring(9, 34).trim();
        float price = Float.parseFloat(line.substring(34).trim());
        return new Product(upc, description, price);
    }
}
